<!DOCTYPE HTML>
<html lang="zh-tw" >
    <!-- Start book python基础语言 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>垃圾回收(三)-gc模块 | python基础语言</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-emphasize/plugin.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../01day/section.4.html" />
    
    
    <link rel="prev" href="../03day/section.0.1.1.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="3.3"
        data-chapter-title="垃圾回收(三)-gc模块"
        data-filepath="03day/section.0.1.2.md"
        data-basepath=".."
        data-revision="Fri Mar 17 2017 08:48:09 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        课程介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="01day/index.html">
            
                
                    <a href="../01day/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        面向对象进阶
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="01day/section.1.0.html">
            
                
                    <a href="../01day/section.1.0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        元类
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="01day/section.1.1.html">
            
                
                    <a href="../01day/section.1.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        python是动态语言
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="01day/section.1.3.html">
            
                
                    <a href="../01day/section.1.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        __slots__
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="02day/section.4.html">
            
                
                    <a href="../02day/section.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        生成器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="02day/section.3.html">
            
                
                    <a href="../02day/section.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        迭代器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="02day/section.1.html">
            
                
                    <a href="../02day/section.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        闭包
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="02day/section.2.html">
            
                
                    <a href="../02day/section.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.7.</b>
                        
                        装饰器
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="02day/index.html">
            
                
                    <a href="../02day/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        其他的知识点
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="03day/section.0.2.html">
            
                
                    <a href="../03day/section.0.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        import导入模块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="03day/section.1.1.html">
            
                
                    <a href="../03day/section.1.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        循环导入
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="03day/section.0.3.html">
            
                
                    <a href="../03day/section.0.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        作用域
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="03day/section.2.html">
            
                
                    <a href="../03day/section.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        ==、is
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="03day/section.1.html">
            
                
                    <a href="../03day/section.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        深拷贝、浅拷贝
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="03day/section.6.html">
            
                
                    <a href="../03day/section.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        进制、位运算
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="01day/section.3.html">
            
                
                    <a href="../01day/section.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.</b>
                        
                        私有化
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="01day/section.3.1.html">
            
                
                    <a href="../01day/section.3.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.8.</b>
                        
                        属性property
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="03day/index.html">
            
                
                    <a href="../03day/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        其他的知识点
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="03day/section.0.1.html">
            
                
                    <a href="../03day/section.0.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        垃圾回收(一)
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="03day/section.0.1.1.html">
            
                
                    <a href="../03day/section.0.1.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        垃圾回收(二)
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="3.3" data-path="03day/section.0.1.2.html">
            
                
                    <a href="../03day/section.0.1.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        垃圾回收(三)-gc模块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="01day/section.4.html">
            
                
                    <a href="../01day/section.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        内建属性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="01day/section.6.html">
            
                
                    <a href="../01day/section.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        内建函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="01day/section.7.html">
            
                
                    <a href="../01day/section.7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        集合set
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="02day/section.5.html">
            
                
                    <a href="../02day/section.5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.7.</b>
                        
                        functools
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="03day/section.4.html">
            
                
                    <a href="../03day/section.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.8.</b>
                        
                        模块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.9" data-path="03day/section.3.html">
            
                
                    <a href="../03day/section.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.9.</b>
                        
                        调试
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.10" data-path="03day/section.5.html">
            
                
                    <a href="../03day/section.5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.10.</b>
                        
                        编码风格
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本書使用 GitBook 釋出
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >python基础语言</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x5783;&#x573E;&#x56DE;&#x6536;&#x4E09;gc&#x6A21;&#x5757;">&#x5783;&#x573E;&#x56DE;&#x6536;(&#x4E09;)-gc&#x6A21;&#x5757;</h1>
<h2 id="&#x4E00;&#x5783;&#x573E;&#x56DE;&#x6536;&#x673A;&#x5236;">&#x4E00;.&#x5783;&#x573E;&#x56DE;&#x6536;&#x673A;&#x5236;</h2>
<p>Python&#x4E2D;&#x7684;&#x5783;&#x573E;&#x56DE;&#x6536;&#x662F;&#x4EE5;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x4E3A;&#x4E3B;&#xFF0C;&#x5206;&#x4EE3;&#x6536;&#x96C6;&#x4E3A;&#x8F85;&#x3002;</p>
<h3 id="1&#x3001;&#x5BFC;&#x81F4;&#x5F15;&#x7528;&#x8BA1;&#x6570;1&#x7684;&#x60C5;&#x51B5;">1&#x3001;&#x5BFC;&#x81F4;&#x5F15;&#x7528;&#x8BA1;&#x6570;+1&#x7684;&#x60C5;&#x51B5;</h3>
<ul>
<li><code>&#x5BF9;&#x8C61;&#x88AB;&#x521B;&#x5EFA;&#xFF0C;&#x4F8B;&#x5982;a=23</code></li>
<li><code>&#x5BF9;&#x8C61;&#x88AB;&#x5F15;&#x7528;&#xFF0C;&#x4F8B;&#x5982;b=a</code></li>
<li><code>&#x5BF9;&#x8C61;&#x88AB;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#xFF0C;&#x4F20;&#x5165;&#x5230;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x4E2D;&#xFF0C;&#x4F8B;&#x5982;func(a)</code></li>
<li><code>&#x5BF9;&#x8C61;&#x4F5C;&#x4E3A;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#xFF0C;&#x5B58;&#x50A8;&#x5728;&#x5BB9;&#x5668;&#x4E2D;&#xFF0C;&#x4F8B;&#x5982;list1=[a,a]</code></li>
</ul>
<h3 id="2&#x3001;&#x5BFC;&#x81F4;&#x5F15;&#x7528;&#x8BA1;&#x6570;1&#x7684;&#x60C5;&#x51B5;">2&#x3001;&#x5BFC;&#x81F4;&#x5F15;&#x7528;&#x8BA1;&#x6570;-1&#x7684;&#x60C5;&#x51B5;</h3>
<ul>
<li><code>&#x5BF9;&#x8C61;&#x7684;&#x522B;&#x540D;&#x88AB;&#x663E;&#x5F0F;&#x9500;&#x6BC1;&#xFF0C;&#x4F8B;&#x5982;del a</code></li>
<li><code>&#x5BF9;&#x8C61;&#x7684;&#x522B;&#x540D;&#x88AB;&#x8D4B;&#x4E88;&#x65B0;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x4F8B;&#x5982;a=24</code></li>
<li><code>&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x79BB;&#x5F00;&#x5B83;&#x7684;&#x4F5C;&#x7528;&#x57DF;&#xFF0C;&#x4F8B;&#x5982;f&#x51FD;&#x6570;&#x6267;&#x884C;&#x5B8C;&#x6BD5;&#x65F6;&#xFF0C;func&#x51FD;&#x6570;&#x4E2D;&#x7684;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#xFF08;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x4E0D;&#x4F1A;&#xFF09;</code></li>
<li><code>&#x5BF9;&#x8C61;&#x6240;&#x5728;&#x7684;&#x5BB9;&#x5668;&#x88AB;&#x9500;&#x6BC1;&#xFF0C;&#x6216;&#x4ECE;&#x5BB9;&#x5668;&#x4E2D;&#x5220;&#x9664;&#x5BF9;&#x8C61;</code></li>
</ul>
<h3 id="3&#x3001;&#x67E5;&#x770B;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x5F15;&#x7528;&#x8BA1;&#x6570;">3&#x3001;&#x67E5;&#x770B;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x5F15;&#x7528;&#x8BA1;&#x6570;</h3>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> sys
a = <span class="hljs-string">&quot;hello world&quot;</span>
sys.getrefcount(a)
</code></pre>
<p>&#x53EF;&#x4EE5;&#x67E5;&#x770B;a&#x5BF9;&#x8C61;&#x7684;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#xFF0C;&#x4F46;&#x662F;&#x6BD4;&#x6B63;&#x5E38;&#x8BA1;&#x6570;&#x5927;1&#xFF0C;&#x56E0;&#x4E3A;&#x8C03;&#x7528;&#x51FD;&#x6570;&#x7684;&#x65F6;&#x5019;&#x4F20;&#x5165;a&#xFF0C;&#x8FD9;&#x4F1A;&#x8BA9;a&#x7684;&#x5F15;&#x7528;&#x8BA1;&#x6570;+1</p>
<h2 id="&#x4E8C;&#x5FAA;&#x73AF;&#x5F15;&#x7528;&#x5BFC;&#x81F4;&#x5185;&#x5B58;&#x6CC4;&#x9732;">&#x4E8C;.&#x5FAA;&#x73AF;&#x5F15;&#x7528;&#x5BFC;&#x81F4;&#x5185;&#x5B58;&#x6CC4;&#x9732;</h2>
<p>&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x7684;&#x7F3A;&#x9677;&#x662F;&#x5FAA;&#x73AF;&#x5F15;&#x7528;&#x7684;&#x95EE;&#x9898;</p>
<pre><code class="lang-python">
<span class="hljs-keyword">import</span> gc

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ClassA</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self)</span>:</span>
        print(<span class="hljs-string">&apos;object born,id:%s&apos;</span>%str(hex(id(self))))

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f2</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">while</span> <span class="hljs-keyword">True</span>:
        c1 = ClassA()
        c2 = ClassA()
        c1.t = c2
        c2.t = c1
        <span class="hljs-keyword">del</span> c1
        <span class="hljs-keyword">del</span> c2

<span class="hljs-comment">#&#x628A;python&#x7684;gc&#x5173;&#x95ED;</span>
gc.disable()

f2()
</code></pre>
<p>&#x6267;&#x884C;f2()&#xFF0C;&#x8FDB;&#x7A0B;&#x5360;&#x7528;&#x7684;&#x5185;&#x5B58;&#x4F1A;&#x4E0D;&#x65AD;&#x589E;&#x5927;&#x3002;</p>
<ul>
<li>&#x521B;&#x5EFA;&#x4E86;c1&#xFF0C;c2&#x540E;&#x8FD9;&#x4E24;&#x5757;&#x5185;&#x5B58;&#x7684;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x90FD;&#x662F;1&#xFF0C;&#x6267;&#x884C;<code>c1.t=c2</code>&#x548C;<code>c2.t=c1</code>&#x540E;&#xFF0C;&#x8FD9;&#x4E24;&#x5757;&#x5185;&#x5B58;&#x7684;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x53D8;&#x6210;2.</li>
<li>&#x5728;del c1&#x540E;&#xFF0C;&#x5185;&#x5B58;1&#x7684;&#x5BF9;&#x8C61;&#x7684;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x53D8;&#x4E3A;1&#xFF0C;&#x7531;&#x4E8E;&#x4E0D;&#x662F;&#x4E3A;0&#xFF0C;&#x6240;&#x4EE5;&#x5185;&#x5B58;1&#x7684;&#x5BF9;&#x8C61;&#x4E0D;&#x4F1A;&#x88AB;&#x9500;&#x6BC1;&#xFF0C;&#x6240;&#x4EE5;&#x5185;&#x5B58;2&#x7684;&#x5BF9;&#x8C61;&#x7684;&#x5F15;&#x7528;&#x6570;&#x4F9D;&#x7136;&#x662F;2&#xFF0C;&#x5728;del c2&#x540E;&#xFF0C;&#x540C;&#x7406;&#xFF0C;&#x5185;&#x5B58;1&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x5185;&#x5B58;2&#x7684;&#x5BF9;&#x8C61;&#x7684;&#x5F15;&#x7528;&#x6570;&#x90FD;&#x662F;1&#x3002;</li>
<li>&#x867D;&#x7136;&#x5B83;&#x4EEC;&#x4E24;&#x4E2A;&#x7684;&#x5BF9;&#x8C61;&#x90FD;&#x662F;&#x53EF;&#x4EE5;&#x88AB;&#x9500;&#x6BC1;&#x7684;&#xFF0C;&#x4F46;&#x662F;&#x7531;&#x4E8E;&#x5FAA;&#x73AF;&#x5F15;&#x7528;&#xFF0C;&#x5BFC;&#x81F4;&#x5783;&#x573E;&#x56DE;&#x6536;&#x5668;&#x90FD;&#x4E0D;&#x4F1A;&#x56DE;&#x6536;&#x5B83;&#x4EEC;&#xFF0C;&#x6240;&#x4EE5;&#x5C31;&#x4F1A;&#x5BFC;&#x81F4;&#x5185;&#x5B58;&#x6CC4;&#x9732;&#x3002;</li>
</ul>
<h2 id="&#x4E09;&#x5783;&#x573E;&#x56DE;&#x6536;">&#x4E09;.&#x5783;&#x573E;&#x56DE;&#x6536;</h2>
<pre><code class="lang-python"><span class="hljs-comment">#coding=utf-8</span>
<span class="hljs-keyword">import</span> gc

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ClassA</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self)</span>:</span>
        print(<span class="hljs-string">&apos;object born,id:%s&apos;</span>%str(hex(id(self))))
    <span class="hljs-comment"># def __del__(self):</span>
    <span class="hljs-comment">#     print(&apos;object del,id:%s&apos;%str(hex(id(self))))</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f3</span><span class="hljs-params">()</span>:</span>
    print(<span class="hljs-string">&quot;-----0------&quot;</span>)
    <span class="hljs-comment"># print(gc.collect())</span>
    c1 = ClassA()
    c2 = ClassA()
    c1.t = c2
    c2.t = c1
    print(<span class="hljs-string">&quot;-----1------&quot;</span>)
    <span class="hljs-keyword">del</span> c1
    <span class="hljs-keyword">del</span> c2
    print(<span class="hljs-string">&quot;-----2------&quot;</span>)
    print(gc.garbage)
    print(<span class="hljs-string">&quot;-----3------&quot;</span>)
    print(gc.collect()) <span class="hljs-comment">#&#x663E;&#x5F0F;&#x6267;&#x884C;&#x5783;&#x573E;&#x56DE;&#x6536;</span>
    print(<span class="hljs-string">&quot;-----4------&quot;</span>)
    print(gc.garbage)
    print(<span class="hljs-string">&quot;-----5------&quot;</span>)

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    gc.set_debug(gc.DEBUG_LEAK) <span class="hljs-comment">#&#x8BBE;&#x7F6E;gc&#x6A21;&#x5757;&#x7684;&#x65E5;&#x5FD7;</span>
    f3()
</code></pre>
<p>python2&#x8FD0;&#x884C;&#x7ED3;&#x679C;:</p>
<pre><code class="lang-python">-----<span class="hljs-number">0</span>------
object born,id:<span class="hljs-number">0x724b20</span>
object born,id:<span class="hljs-number">0x724b48</span>
-----<span class="hljs-number">1</span>------
-----<span class="hljs-number">2</span>------
[]
-----<span class="hljs-number">3</span>------
gc: collectable &lt;ClassA instance at <span class="hljs-number">0x724b20</span>&gt;
gc: collectable &lt;ClassA instance at <span class="hljs-number">0x724b48</span>&gt;
gc: collectable &lt;dict <span class="hljs-number">0x723300</span>&gt;
gc: collectable &lt;dict <span class="hljs-number">0x71bf60</span>&gt;
<span class="hljs-number">4</span>
-----<span class="hljs-number">4</span>------
[&lt;__main__.ClassA instance at <span class="hljs-number">0x724b20</span>&gt;, &lt;__main__.ClassA instance at <span class="hljs-number">0x724b48</span>&gt;, {<span class="hljs-string">&apos;t&apos;</span>: &lt;__main__.ClassA instance at <span class="hljs-number">0x724b48</span>&gt;}, {<span class="hljs-string">&apos;t&apos;</span>: &lt;__main__.ClassA instance at <span class="hljs-number">0x724b20</span>&gt;}]
-----<span class="hljs-number">5</span>------
</code></pre>
<h5 id="&#x8BF4;&#x660E;">&#x8BF4;&#x660E;:</h5>
<ul>
<li>&#x5783;&#x573E;&#x56DE;&#x6536;&#x540E;&#x7684;&#x5BF9;&#x8C61;&#x4F1A;&#x653E;&#x5728;gc.garbage&#x5217;&#x8868;&#x91CC;&#x9762;</li>
<li>gc.collect()&#x4F1A;&#x8FD4;&#x56DE;&#x4E0D;&#x53EF;&#x8FBE;&#x7684;&#x5BF9;&#x8C61;&#x6570;&#x76EE;&#xFF0C;4&#x7B49;&#x4E8E;&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#x4EE5;&#x53CA;&#x5B83;&#x4EEC;&#x5BF9;&#x5E94;&#x7684;dict</li>
</ul>
<h3 id="&#x6709;&#x4E09;&#x79CD;&#x60C5;&#x51B5;&#x4F1A;&#x89E6;&#x53D1;&#x5783;&#x573E;&#x56DE;&#x6536;&#xFF1A;">&#x6709;&#x4E09;&#x79CD;&#x60C5;&#x51B5;&#x4F1A;&#x89E6;&#x53D1;&#x5783;&#x573E;&#x56DE;&#x6536;&#xFF1A;</h3>
<ol>
<li>&#x8C03;&#x7528;gc.collect(),</li>
<li>&#x5F53;gc&#x6A21;&#x5757;&#x7684;&#x8BA1;&#x6570;&#x5668;&#x8FBE;&#x5230;&#x9600;&#x503C;&#x7684;&#x65F6;&#x5019;&#x3002;</li>
<li>&#x7A0B;&#x5E8F;&#x9000;&#x51FA;&#x7684;&#x65F6;&#x5019;</li>
</ol>
<h2 id="&#x56DB;gc&#x6A21;&#x5757;&#x5E38;&#x7528;&#x529F;&#x80FD;&#x89E3;&#x6790;">&#x56DB;.gc&#x6A21;&#x5757;&#x5E38;&#x7528;&#x529F;&#x80FD;&#x89E3;&#x6790;</h2>
<p><code>gc&#x6A21;&#x5757;&#x63D0;&#x4F9B;&#x4E00;&#x4E2A;&#x63A5;&#x53E3;&#x7ED9;&#x5F00;&#x53D1;&#x8005;&#x8BBE;&#x7F6E;&#x5783;&#x573E;&#x56DE;&#x6536;&#x7684;&#x9009;&#x9879;</code>&#x3002;&#x4E0A;&#x9762;&#x8BF4;&#x5230;&#xFF0C;&#x91C7;&#x7528;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x7684;&#x65B9;&#x6CD5;&#x7BA1;&#x7406;&#x5185;&#x5B58;&#x7684;&#x4E00;&#x4E2A;&#x7F3A;&#x9677;&#x662F;&#x5FAA;&#x73AF;&#x5F15;&#x7528;&#xFF0C;&#x800C;gc&#x6A21;&#x5757;&#x7684;&#x4E00;&#x4E2A;&#x4E3B;&#x8981;&#x529F;&#x80FD;&#x5C31;&#x662F;&#x89E3;&#x51B3;&#x5FAA;&#x73AF;&#x5F15;&#x7528;&#x7684;&#x95EE;&#x9898;&#x3002;</p>
<h5 id="&#x5E38;&#x7528;&#x51FD;&#x6570;&#xFF1A;">&#x5E38;&#x7528;&#x51FD;&#x6570;&#xFF1A;</h5>
<p>1&#x3001;gc.set_debug(flags)
&#x8BBE;&#x7F6E;gc&#x7684;debug&#x65E5;&#x5FD7;&#xFF0C;&#x4E00;&#x822C;&#x8BBE;&#x7F6E;&#x4E3A;gc.DEBUG_LEAK</p>
<p>2&#x3001;gc.collect([generation])
&#x663E;&#x5F0F;&#x8FDB;&#x884C;&#x5783;&#x573E;&#x56DE;&#x6536;&#xFF0C;&#x53EF;&#x4EE5;&#x8F93;&#x5165;&#x53C2;&#x6570;&#xFF0C;0&#x4EE3;&#x8868;&#x53EA;&#x68C0;&#x67E5;&#x7B2C;&#x4E00;&#x4EE3;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;1&#x4EE3;&#x8868;&#x68C0;&#x67E5;&#x4E00;&#xFF0C;&#x4E8C;&#x4EE3;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;2&#x4EE3;&#x8868;&#x68C0;&#x67E5;&#x4E00;&#xFF0C;&#x4E8C;&#xFF0C;&#x4E09;&#x4EE3;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x4F20;&#x53C2;&#x6570;&#xFF0C;&#x6267;&#x884C;&#x4E00;&#x4E2A;full collection&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x7B49;&#x4E8E;&#x4F20;2&#x3002;
&#x8FD4;&#x56DE;&#x4E0D;&#x53EF;&#x8FBE;&#xFF08;unreachable objects&#xFF09;&#x5BF9;&#x8C61;&#x7684;&#x6570;&#x76EE;</p>
<p>3&#x3001;gc.get_threshold()
&#x83B7;&#x53D6;&#x7684;gc&#x6A21;&#x5757;&#x4E2D;&#x81EA;&#x52A8;&#x6267;&#x884C;&#x5783;&#x573E;&#x56DE;&#x6536;&#x7684;&#x9891;&#x7387;&#x3002;</p>
<p>4&#x3001;gc.set_threshold(threshold0[, threshold1[, threshold2])
&#x8BBE;&#x7F6E;&#x81EA;&#x52A8;&#x6267;&#x884C;&#x5783;&#x573E;&#x56DE;&#x6536;&#x7684;&#x9891;&#x7387;&#x3002;</p>
<p>5&#x3001;gc.get_count()
&#x83B7;&#x53D6;&#x5F53;&#x524D;&#x81EA;&#x52A8;&#x6267;&#x884C;&#x5783;&#x573E;&#x56DE;&#x6536;&#x7684;&#x8BA1;&#x6570;&#x5668;&#xFF0C;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x957F;&#x5EA6;&#x4E3A;3&#x7684;&#x5217;&#x8868;</p>
<h4 id="gc&#x6A21;&#x5757;&#x7684;&#x81EA;&#x52A8;&#x5783;&#x573E;&#x56DE;&#x6536;&#x673A;&#x5236;">gc&#x6A21;&#x5757;&#x7684;&#x81EA;&#x52A8;&#x5783;&#x573E;&#x56DE;&#x6536;&#x673A;&#x5236;</h4>
<p>&#x5FC5;&#x987B;&#x8981;import gc&#x6A21;&#x5757;&#xFF0C;&#x5E76;&#x4E14;<code>is_enable()=True</code>&#x624D;&#x4F1A;&#x542F;&#x52A8;&#x81EA;&#x52A8;&#x5783;&#x573E;&#x56DE;&#x6536;&#x3002;</p>
<p>&#x8FD9;&#x4E2A;&#x673A;&#x5236;&#x7684;<code>&#x4E3B;&#x8981;&#x4F5C;&#x7528;&#x5C31;&#x662F;&#x53D1;&#x73B0;&#x5E76;&#x5904;&#x7406;&#x4E0D;&#x53EF;&#x8FBE;&#x7684;&#x5783;&#x573E;&#x5BF9;&#x8C61;</code>&#x3002;</p>
<p><code>&#x5783;&#x573E;&#x56DE;&#x6536;=&#x5783;&#x573E;&#x68C0;&#x67E5;+&#x5783;&#x573E;&#x56DE;&#x6536;</code></p>
<p>&#x5728;Python&#x4E2D;&#xFF0C;&#x91C7;&#x7528;&#x5206;&#x4EE3;&#x6536;&#x96C6;&#x7684;&#x65B9;&#x6CD5;&#x3002;&#x628A;&#x5BF9;&#x8C61;&#x5206;&#x4E3A;&#x4E09;&#x4EE3;&#xFF0C;&#x4E00;&#x5F00;&#x59CB;&#xFF0C;&#x5BF9;&#x8C61;&#x5728;&#x521B;&#x5EFA;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x653E;&#x5728;&#x4E00;&#x4EE3;&#x4E2D;&#xFF0C;&#x5982;&#x679C;&#x5728;&#x4E00;&#x6B21;&#x4E00;&#x4EE3;&#x7684;&#x5783;&#x573E;&#x68C0;&#x67E5;&#x4E2D;&#xFF0C;&#x6539;&#x5BF9;&#x8C61;&#x5B58;&#x6D3B;&#x4E0B;&#x6765;&#xFF0C;&#x5C31;&#x4F1A;&#x88AB;&#x653E;&#x5230;&#x4E8C;&#x4EE3;&#x4E2D;&#xFF0C;&#x540C;&#x7406;&#x5728;&#x4E00;&#x6B21;&#x4E8C;&#x4EE3;&#x7684;&#x5783;&#x573E;&#x68C0;&#x67E5;&#x4E2D;&#xFF0C;&#x8BE5;&#x5BF9;&#x8C61;&#x5B58;&#x6D3B;&#x4E0B;&#x6765;&#xFF0C;&#x5C31;&#x4F1A;&#x88AB;&#x653E;&#x5230;&#x4E09;&#x4EE3;&#x4E2D;&#x3002;</p>
<p>gc&#x6A21;&#x5757;&#x91CC;&#x9762;&#x4F1A;&#x6709;&#x4E00;&#x4E2A;&#x957F;&#x5EA6;&#x4E3A;3&#x7684;&#x5217;&#x8868;&#x7684;&#x8BA1;&#x6570;&#x5668;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;gc.get_count()&#x83B7;&#x53D6;&#x3002;</p>
<p>&#x4F8B;&#x5982;(488,3,0)&#xFF0C;&#x5176;&#x4E2D;488&#x662F;&#x6307;&#x8DDD;&#x79BB;&#x4E0A;&#x4E00;&#x6B21;&#x4E00;&#x4EE3;&#x5783;&#x573E;&#x68C0;&#x67E5;&#xFF0C;Python&#x5206;&#x914D;&#x5185;&#x5B58;&#x7684;&#x6570;&#x76EE;&#x51CF;&#x53BB;&#x91CA;&#x653E;&#x5185;&#x5B58;&#x7684;&#x6570;&#x76EE;&#xFF0C;&#x6CE8;&#x610F;&#x662F;&#x5185;&#x5B58;&#x5206;&#x914D;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x7684;&#x589E;&#x52A0;&#x3002;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">print</span> gc.get_count() <span class="hljs-comment"># (590, 8, 0)</span>
a = ClassA()
<span class="hljs-keyword">print</span> gc.get_count() <span class="hljs-comment"># (591, 8, 0)</span>
<span class="hljs-keyword">del</span> a
<span class="hljs-keyword">print</span> gc.get_count() <span class="hljs-comment"># (590, 8, 0)</span>
</code></pre>
<p>3&#x662F;&#x6307;&#x8DDD;&#x79BB;&#x4E0A;&#x4E00;&#x6B21;&#x4E8C;&#x4EE3;&#x5783;&#x573E;&#x68C0;&#x67E5;&#xFF0C;&#x4E00;&#x4EE3;&#x5783;&#x573E;&#x68C0;&#x67E5;&#x7684;&#x6B21;&#x6570;&#xFF0C;&#x540C;&#x7406;&#xFF0C;0&#x662F;&#x6307;&#x8DDD;&#x79BB;&#x4E0A;&#x4E00;&#x6B21;&#x4E09;&#x4EE3;&#x5783;&#x573E;&#x68C0;&#x67E5;&#xFF0C;&#x4E8C;&#x4EE3;&#x5783;&#x573E;&#x68C0;&#x67E5;&#x7684;&#x6B21;&#x6570;&#x3002;</p>
<p>gc&#x6A21;&#x5FEB;&#x6709;&#x4E00;&#x4E2A;&#x81EA;&#x52A8;&#x5783;&#x573E;&#x56DE;&#x6536;&#x7684;<code>&#x9600;&#x503C;</code>&#xFF0C;&#x5373;&#x901A;&#x8FC7;gc.get_threshold&#x51FD;&#x6570;&#x83B7;&#x53D6;&#x5230;&#x7684;&#x957F;&#x5EA6;&#x4E3A;3&#x7684;&#x5143;&#x7EC4;&#xFF0C;&#x4F8B;&#x5982;(700,10,10)
&#x6BCF;&#x4E00;&#x6B21;&#x8BA1;&#x6570;&#x5668;&#x7684;&#x589E;&#x52A0;&#xFF0C;gc&#x6A21;&#x5757;&#x5C31;&#x4F1A;&#x68C0;&#x67E5;&#x589E;&#x52A0;&#x540E;&#x7684;&#x8BA1;&#x6570;&#x662F;&#x5426;&#x8FBE;&#x5230;&#x9600;&#x503C;&#x7684;&#x6570;&#x76EE;&#xFF0C;&#x5982;&#x679C;&#x662F;&#xFF0C;&#x5C31;&#x4F1A;&#x6267;&#x884C;&#x5BF9;&#x5E94;&#x7684;&#x4EE3;&#x6570;&#x7684;&#x5783;&#x573E;&#x68C0;&#x67E5;&#xFF0C;&#x7136;&#x540E;&#x91CD;&#x7F6E;&#x8BA1;&#x6570;&#x5668;</p>
<p>&#x4F8B;&#x5982;&#xFF0C;&#x5047;&#x8BBE;&#x9600;&#x503C;&#x662F;(700,10,10)&#xFF1A;</p>
<pre><code class="lang-python">&#x5F53;&#x8BA1;&#x6570;&#x5668;&#x4ECE;(<span class="hljs-number">699</span>,<span class="hljs-number">3</span>,<span class="hljs-number">0</span>)&#x589E;&#x52A0;&#x5230;(<span class="hljs-number">700</span>,<span class="hljs-number">3</span>,<span class="hljs-number">0</span>)&#xFF0C;gc&#x6A21;&#x5757;&#x5C31;&#x4F1A;&#x6267;&#x884C;gc.collect(<span class="hljs-number">0</span>),&#x5373;&#x68C0;&#x67E5;&#x4E00;&#x4EE3;&#x5BF9;&#x8C61;&#x7684;&#x5783;&#x573E;&#xFF0C;&#x5E76;&#x91CD;&#x7F6E;&#x8BA1;&#x6570;&#x5668;&#x4E3A;(<span class="hljs-number">0</span>,<span class="hljs-number">4</span>,<span class="hljs-number">0</span>)
&#x5F53;&#x8BA1;&#x6570;&#x5668;&#x4ECE;(<span class="hljs-number">699</span>,<span class="hljs-number">9</span>,<span class="hljs-number">0</span>)&#x589E;&#x52A0;&#x5230;(<span class="hljs-number">700</span>,<span class="hljs-number">9</span>,<span class="hljs-number">0</span>)&#xFF0C;gc&#x6A21;&#x5757;&#x5C31;&#x4F1A;&#x6267;&#x884C;gc.collect(<span class="hljs-number">1</span>),&#x5373;&#x68C0;&#x67E5;&#x4E00;&#x3001;&#x4E8C;&#x4EE3;&#x5BF9;&#x8C61;&#x7684;&#x5783;&#x573E;&#xFF0C;&#x5E76;&#x91CD;&#x7F6E;&#x8BA1;&#x6570;&#x5668;&#x4E3A;(<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">1</span>)
&#x5F53;&#x8BA1;&#x6570;&#x5668;&#x4ECE;(<span class="hljs-number">699</span>,<span class="hljs-number">9</span>,<span class="hljs-number">9</span>)&#x589E;&#x52A0;&#x5230;(<span class="hljs-number">700</span>,<span class="hljs-number">9</span>,<span class="hljs-number">9</span>)&#xFF0C;gc&#x6A21;&#x5757;&#x5C31;&#x4F1A;&#x6267;&#x884C;gc.collect(<span class="hljs-number">2</span>),&#x5373;&#x68C0;&#x67E5;&#x4E00;&#x3001;&#x4E8C;&#x3001;&#x4E09;&#x4EE3;&#x5BF9;&#x8C61;&#x7684;&#x5783;&#x573E;&#xFF0C;&#x5E76;&#x91CD;&#x7F6E;&#x8BA1;&#x6570;&#x5668;&#x4E3A;(<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>)
</code></pre>
<h2 id="&#x6CE8;&#x610F;&#x70B9;">&#x6CE8;&#x610F;&#x70B9;</h2>
<p>gc&#x6A21;&#x5757;&#x552F;&#x4E00;&#x5904;&#x7406;&#x4E0D;&#x4E86;&#x7684;&#x662F;&#x5FAA;&#x73AF;&#x5F15;&#x7528;&#x7684;&#x7C7B;&#x90FD;&#x6709;__del__&#x65B9;&#x6CD5;&#xFF0C;&#x6240;&#x4EE5;&#x9879;&#x76EE;&#x4E2D;&#x8981;&#x907F;&#x514D;&#x5B9A;&#x4E49;__del__&#x65B9;&#x6CD5;</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> gc

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ClassA</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">pass</span>
    <span class="hljs-comment"># def __del__(self):</span>
    <span class="hljs-comment">#     print(&apos;object born,id:%s&apos;%str(hex(id(self))))</span>

gc.set_debug(gc.DEBUG_LEAK)
a = ClassA()
b = ClassA()

a.next = b
b.prev = a

<span class="hljs-keyword">print</span> <span class="hljs-string">&quot;--1--&quot;</span>
<span class="hljs-keyword">print</span> gc.collect()
<span class="hljs-keyword">print</span> <span class="hljs-string">&quot;--2--&quot;</span>
<span class="hljs-keyword">del</span> a
<span class="hljs-keyword">print</span> <span class="hljs-string">&quot;--3--&quot;</span>
<span class="hljs-keyword">del</span> b
<span class="hljs-keyword">print</span> <span class="hljs-string">&quot;--3-1--&quot;</span>
<span class="hljs-keyword">print</span> gc.collect()
<span class="hljs-keyword">print</span> <span class="hljs-string">&quot;--4--&quot;</span>
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python">--<span class="hljs-number">1</span>--
<span class="hljs-number">0</span>
--<span class="hljs-number">2</span>--
--<span class="hljs-number">3</span>--
--<span class="hljs-number">3</span>-<span class="hljs-number">1</span>--
gc: collectable &lt;ClassA instance at <span class="hljs-number">0x21248c8</span>&gt;
gc: collectable &lt;ClassA instance at <span class="hljs-number">0x21248f0</span>&gt;
gc: collectable &lt;dict <span class="hljs-number">0x2123030</span>&gt;
gc: collectable &lt;dict <span class="hljs-number">0x2123150</span>&gt;
<span class="hljs-number">4</span>
--<span class="hljs-number">4</span>--
</code></pre>
<p>&#x5982;&#x679C;&#x628A;<strong>del</strong>&#x6253;&#x5F00;&#xFF0C;&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x4E3A;:</p>
<pre><code class="lang-python">--<span class="hljs-number">1</span>--
<span class="hljs-number">0</span>
--<span class="hljs-number">2</span>--
--<span class="hljs-number">3</span>--
--<span class="hljs-number">3</span>-<span class="hljs-number">1</span>--
gc: uncollectable &lt;ClassA instance at <span class="hljs-number">0x6269b8</span>&gt;
gc: uncollectable &lt;ClassA instance at <span class="hljs-number">0x6269e0</span>&gt;
gc: uncollectable &lt;dict <span class="hljs-number">0x61bed0</span>&gt;
gc: uncollectable &lt;dict <span class="hljs-number">0x6230c0</span>&gt;
<span class="hljs-number">4</span>
--<span class="hljs-number">4</span>--
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../03day/section.0.1.1.html" class="navigation navigation-prev " aria-label="Previous page: 垃圾回收(二)"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../01day/section.4.html" class="navigation navigation-next " aria-label="Next page: 内建属性"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-livereload/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"toggle-chapters":{},"splitter":{},"emphasize":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"livereload":{}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book python基础语言 -->
</html>
